/** 
 * @Title: SentenceUtil.java 
 * @Package com.zizo.autohome.sentence.util 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月18日 上午10:46:46 
 */
package com.zizo.autohome.sentence.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: SentenceUtil
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月18日 上午10:46:46
 * 
 */
public class SentenceUtil {

	public Map<List<String>, String> getWord(String database, String tablename) {
		Connection conn = ConnectionPool.getConnection(database, "localhost");
		Map<List<String>, String> map = new HashMap<>();
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT ifnull(w1,''),ifnull(w2,''),ifnull(w3,''),ifnull(w4,''),ifnull(comment,'') from "
				+ tablename;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String w1 = rs.getString(1);
				String w2 = rs.getString(2);
				String w3 = rs.getString(3);
				String w4 = rs.getString(4);
				String coment = rs.getString(5);
				List<String> list = new ArrayList<>();
				list.add(w1);
				list.add(w2);
				list.add(w3);
				list.add(w4);
				map.put(list, coment);
				list = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * @Title: readWord
	 * @Description: TODO 读关键词集合
	 * @param wordpath
	 * @param count
	 * @return
	 * @throws IOException
	 * @return List<List<String>>
	 * @author Alexis
	 */
	public final List<List<String>> readWord(String wordpath, int count) throws IOException {
		List<List<String>> list = new ArrayList<>();
		InputStream is = this.getClass().getResourceAsStream("word1.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		List<String> tempList = new ArrayList<>();
		String temp;
		while ((temp = br.readLine()) != null) {
			tempList.add(temp);
		}
		for (String temp1 : tempList) {
			List<String> word = new ArrayList<>();
			String[] words = temp1.split("\\s+");
			for (String s : words) {
				word.add(s);
			}
			for (int i = words.length; i < count; i++) {
				word.add("");
			}
			list.add(word);
		}
		return list;
	}

	/**
	 * @Title: readWord
	 * @Description: TODO 读关键词集合
	 * @param wordpath
	 * @param count
	 * @return
	 * @throws IOException
	 * @return List<List<String>>
	 * @author Alexis
	 */
	public final List<List<String>> readWord2(String wordpath, int count) throws IOException {
		List<List<String>> list = new ArrayList<>();
		InputStream is = this.getClass().getResourceAsStream("word2.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		List<String> tempList = new ArrayList<>();
		String temp;
		while ((temp = br.readLine()) != null) {
			tempList.add(temp);
		}
		for (String temp1 : tempList) {
			List<String> word = new ArrayList<>();
			String[] words = temp1.split("\\s+");
			for (String s : words) {
				word.add(s);
			}
			for (int i = words.length; i < count; i++) {
				word.add("");
			}
			list.add(word);
		}
		return list;
	}

	/**
	 * @Title: readCarID
	 * @Description: TODO 获取carID
	 * @param carpath
	 * @return
	 * @throws IOException
	 * @return List<Integer>
	 * @author Alexis
	 */
	public final List<Integer> readCarID(String carpath) throws IOException {
		List<Integer> list = new ArrayList<>();
		InputStream is = this.getClass().getResourceAsStream("car.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String temp = "";
		while ((temp = br.readLine()) != null) {
			list.add(Integer.parseInt(temp.trim()));
		}
		return list;
	}

	/**
	 * @Title: readYear
	 * @Description: TODO 获取年份
	 * @param yearpath
	 * @return
	 * @throws IOException
	 * @return List<Integer>
	 * @author Alexis
	 */
	public final List<Integer> readYear(String yearpath) throws IOException {
		List<Integer> list = new ArrayList<>();
		InputStream is = this.getClass().getResourceAsStream("year.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String temp = "";
		while ((temp = br.readLine()) != null) {
			list.add(Integer.parseInt(temp.trim()));
		}
		return list;
	}

	/**
	 * @Title: createTable
	 * @Description: TODO 建表
	 * @param tablename
	 * @param count
	 * @param database
	 * @return void
	 * @author Alexis
	 */
	public final static void createTable(String tablename, int count, int count2, String database, boolean isFilter) {
		Connection conn = ConnectionPool.getConnection(database, "localhost");
		StringBuffer sb = new StringBuffer();
		sb.append("CREATE TABLE `" + tablename + "` (" + "`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键，自动增长',"
				+ "`carid` int(11) DEFAULT NULL COMMENT '车款ID',");
		for (int i = 0; i < count; i++) {
			int j = i + 1;
			sb.append(" `w" + j + "` varchar(200) DEFAULT NULL COMMENT 'Car关键词" + j + "',");
		}
		sb.append("`comment1` varchar(200) DEFAULT NULL COMMENT 'word1 注释',");
		if (isFilter) {
			for (int i = 0; i < count2; i++) {
				int j = i + 1;
				sb.append(" `t" + j + "` varchar(200) DEFAULT NULL COMMENT 'Topic关键词" + j + "',");
			}
			sb.append("`comment2` varchar(200) DEFAULT NULL COMMENT 'word2 注释',");
		}
		sb.append(
				"`content` text DEFAULT NULL COMMENT '句子',`year` int(11) DEFAULT NULL COMMENT '年份',`area` varchar(200) DEFAULT NULL COMMENT '地区线级',`pid` int(11) DEFAULT NULL COMMENT '帖子ID',`type` int(11)  COMMENT '语料类型 1--bbs,2--kb,3--news',"
						+ " PRIMARY KEY (`id`)" + ") ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;");
		/** 建表 **/
		Statement st = null;
		try {
			st = conn.createStatement();
			st.executeUpdate("DROP TABLE IF EXISTS " + tablename + ";");
			st.executeUpdate(sb.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Title: getCityLevel
	 * @Description: TODO 得到城市的级别
	 * @return
	 * @return Map<String,String>
	 * @author Alexis
	 */
	public final static Map<String, String> getCityLevel() {
		Map<String, String> map = new HashMap<>();
		String sql = "SELECT area_city,area_class FROM core_area WHERE LENGTH(area_city)>0";
		Connection conn = ConnectionPool.getConnection("carvoice_core", "localhost");
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String city = rs.getString(1);
				String level = rs.getString(2);
				map.put(city, level);
			}
			map.put("", "");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static void main(String[] args) throws IOException {
	}
}
